﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Browse_SeriesDetail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //query is the Series Id from the Series Selected in the Previous Page used for filtering all of the searches
        if (Request.QueryString["query"] != null)
        {
            Int32 SelectedThreadID = Int32.Parse(Request.QueryString["query"]);

            //Reading the Series Table to get all of the information about the user selected series
            using (PublicCatalogDataContext db = new PublicCatalogDataContext())
            {
                var mySeries = from Series in db.Series
                               where Series.SeriesId == SelectedThreadID
                               select new
                               {
                                   Series.SeriesIdentifier,
                                   Series.SeriesTitle,
                                   Series.SeriesDateRangeDescription,
                                   Series.SeriesEndYear,
                                   Series.SeriesStartYear,
                                   Series.SeriesExtentDescription,
                                   Series.SeriesArrangement,
                                   Series.SeriesDescription,
                                   Series.SeriesGeneralNote,
                                   Series.SeriesId
                               };
                foreach (var a in mySeries)
                {
                    var SeriesTitle = a.SeriesTitle;
                    Literal1.Text = a.SeriesTitle;
                    Repeater1.DataSource = mySeries;
                    Repeater1.DataBind();
                    Repeater3.DataSource = mySeries;
                    Repeater3.DataBind();
                }
            }

            // Gets the Keywords for the Series
            using (PublicCatalogDataContext db = new PublicCatalogDataContext())
            {
                var myKeyword = from SeriesKeyword in db.SeriesKeywords
                                where SeriesKeyword.SeriesId == SelectedThreadID
                                select new
                                {
                                    SeriesKeyword.SeriesKeyword1
                                };
                Repeater4.DataSource = myKeyword;
                Repeater4.DataBind();
            }
            // Get the Geographic Location for the Series
            using (PublicCatalogDataContext db = new PublicCatalogDataContext())
            {
                var myGeoLocation = from a in db.SeriesGeoLocation_LINKs
                                    join b in db.GeoLocations on a.GeoLocationId equals b.GeoLocationId
                                    where a.SeriesId == SelectedThreadID
                                    select new
                                    {
                                        b.GeoLocationValue
                                    };
                Repeater2.DataSource = myGeoLocation;
                Repeater2.DataBind();
            }


            //Gets the Subjects for the series by joining the SeriesSubject_LINK table to the
            //SeriesSubject Table and filtering on SeriesId to get the Subjects for the Selected Series
            //Then read the Qualifier table to get the QualifierType for that Subject.
            using (PublicCatalogDataContext db = new PublicCatalogDataContext())
            {
                var mySubjects = from p in db.SeriesSubject_LINKs
                                 join cp in db.Subjects on p.SubjectId equals cp.SubjectId
                                 where p.SeriesId == SelectedThreadID
                                 join c in db.Qualifiers on p.QualifierId equals c.QualifierId
                                 select new
                                 {
                                     p.SeriesId,
                                     cp.SubjectName,
                                     c.QualifierType
                                 };
                Repeater5.DataSource = mySubjects;
                Repeater5.DataBind();
            }
        }
        else
        {
            Label1.Text = "Unable to Locate the Selected Series Please Try Again Later.";
            Label1.Visible = true;
        }
    }
}


//public partial class Browse_SeriesDetail : System.Web.UI.Page
//{
//    private int RowCount
//    {
//        get
//        {
//            return (int)ViewState["RowCount"];
//        }
//        set
//        {
//            ViewState["RowCount"] = value;
//        }
//    }

//    protected void Page_Load(object sender, EventArgs e)
//    {
//        if (!IsPostBack)
//        {
//            FetchData(30, 0);
//        }
//        else
//        {
//            plcPaging.Controls.Clear();
//            CreatePagingControl();
//        }
//    }

//    private void FetchData(int take, int pageSize)
//    {
//        // query is the Series Id from the Series Selected in the Previous Page used for filtering all of the searches
//        if (Request.QueryString["query"] != null)
//        {
//            Int32 SelectedThreadID = Int32.Parse(Request.QueryString["query"]);

//            //Reading the Series Table to get all of the information about the user selected series
//            using (PublicCatalogDataContext db = new PublicCatalogDataContext())
//            {
//                var mySeries = from Series in db.Series
//                               where Series.SeriesId == SelectedThreadID
//                               select new
//                               {
//                                   Series.SeriesIdentifier,
//                                   Series.SeriesTitle,
//                                   Series.SeriesDateRangeDescription,
//                                   Series.SeriesEndYear,
//                                   Series.SeriesStartYear,
//                                   Series.SeriesExtentDescription,
//                                   Series.SeriesArrangement,
//                                   Series.SeriesDescription,
//                                   Series.SeriesGeneralNote
//                               };
//                foreach (var a in mySeries)
//                {
//                    var SeriesTitle = a.SeriesTitle;
//                    Literal1.Text = a.SeriesTitle;
//                    Repeater1.DataSource = mySeries;
//                    Repeater1.DataBind();
//                }
//            }

//            // Gets the Keywords for the Series
//            using (PublicCatalogDataContext db = new PublicCatalogDataContext())
//            {
//                var myKeyword = from SeriesKeyword in db.SeriesKeywords
//                                    where SeriesKeyword.SeriesId == SelectedThreadID
//                                    select new
//                                    {
//                                        SeriesKeyword.SeriesKeyword1
//                                    };
//                Repeater4.DataSource = myKeyword;
//                Repeater4.DataBind();
//            }
//            // Get the Geographic Location for the Series
//            using (PublicCatalogDataContext db = new PublicCatalogDataContext())
//            {
//                var myGeoLocation = from a in db.SeriesGeoLocation_LINKs
//                                join b in db.GeoLocations on a.GeoLocationId equals b.GeoLocationId
//                                where a.SeriesId == SelectedThreadID
//                                select new
//                                {
//                                    b.GeoLocationValue
//                                };
//                Repeater2.DataSource = myGeoLocation;
//                Repeater2.DataBind();
//            }


//            //Gets the Subjects for the series by joining the SeriesSubject_LINK table to the
//            //SeriesSubject Table and filtering on SeriesId to get the Subjects for the Selected Series
//            //Then read the Qualifier table to get the QualifierType for that Subject.
//            using (PublicCatalogDataContext db = new PublicCatalogDataContext())
//            {
//                var mySubjects = from p in db.SeriesSubject_LINKs
//                                    join cp in db.Subjects on p.SubjectId equals cp.SubjectId
//                                    where p.SeriesId == SelectedThreadID
//                                    join c in db.Qualifiers on p.QualifierId equals c.QualifierId
//                                    select new
//                                    {
//                                        p.SeriesId,
//                                        cp.SubjectName,
//                                        c.QualifierType
//                                    };
//                ListView1.DataSource = mySubjects;
//                ListView1.DataBind();                
//            }

//            //using (PublicCatalogDataContext db = new PublicCatalogDataContext())
//            //{
//            //    var myItems = from x in db.Items
//            //                  where (x.SeriesId == SelectedThreadID)
//            //                  select new
//            //                  {
//            //                      x.ItemOpenWebDate
//            //                  };
//            //    foreach (var x in myItems)
//            //    {
//            //        if (x.ItemOpenWebDate = "NULL")
//            //        {
//            //            Label1.Text = "Unable to Locate Items Please Try Again Later.";
//            //            Label1.Visible = true;
//            //        }
//            //    } 
//            //}


//            //Returns all of the Items for a selected series          

//            using (PublicCatalogDataContext db = new PublicCatalogDataContext())
//            {
//                //.Where(a => a.SeriesId == SelectedThreadID & a.ItemOpenWebDate != null)
//                var query = from q in db.Items
//                                .Where(a => a.SeriesId == SelectedThreadID)
//                                    //.OrderBy(o => o.FilingUnitId)
//                                    .Take(take)
//                                    .Skip(pageSize)
//                            select new
//                            {
//                             ID = q.ItemId,
//                             Name = q.ItemTitle,
//                             Count = (from x in db.Items
//                             where x.SeriesId == SelectedThreadID
//                             select x).Count()
//                            };

//                PagedDataSource page = new PagedDataSource();
//                page.AllowCustomPaging = true;
//                page.AllowPaging = true;
//                page.DataSource = query;
//                page.PageSize = 30;
//                Repeater3.DataSource = page;
//                Repeater3.DataBind();

//                if (!IsPostBack)
//                {
//                    RowCount = query.First().Count;
//                    CreatePagingControl();
//                }
//            }
//        }
//            //If the query value passed from the previous page is null then the error below will display.
//        else
//        {
//            Label1.Text = "Unable to Locate the Selected Series Please Try Again Later.";
//            Label1.Visible = true;
//        }
//    }

//    private void CreatePagingControl()
//    {
//        for (int i = 0; i < (RowCount / 30) + 1; i++)
//        {
//            LinkButton lnk = new LinkButton();
//            lnk.Click += new EventHandler(lbl_Click);
//            lnk.ID = "lnkPage" + (i + 1).ToString();
//            lnk.Text = (i + 1).ToString();
//            plcPaging.Controls.Add(lnk);
//            Label spacer = new Label();
//            spacer.Text = "&nbsp;";
//            plcPaging.Controls.Add(spacer);
//        }
//    }

//    void lbl_Click(object sender, EventArgs e)
//    {
//        LinkButton lnk = sender as LinkButton;
//        int currentPage = int.Parse(lnk.Text);
//        int take = currentPage * 30;
//        int skip = currentPage == 1 ? 0 : take - 30;
//        FetchData(take, skip);
//    }
//}
